package com.example.sample.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.ny.nybase.widget.ptr.OnFooterRefreshListener;
import com.ny.nybase.widget.ptr.OnHeaderRefreshListener;
import com.ny.nybase.widget.ptr.PullToRefreshListView;
import com.ny.nybase.widget.ptr.PullToRefreshWidget;
import com.ny.nybase.widget.ptr.PullToRefreshWidgetState;

import net.liteheaven.xblib.common.ui.ToastUtil;

import androidx.annotation.Nullable;

import static android.view.Gravity.BOTTOM;
import static android.view.Gravity.CENTER;
import static android.view.Gravity.CENTER_HORIZONTAL;
import static android.view.Gravity.TOP;

/**
 * Created by LiangYu on 2018/1/5.
 */
public class PullToRefreshListViewActivity extends BaseActivity {

    private CheckBox cbScrollbar;
    private CheckBox cbHeaderPull;
    private CheckBox cbFooterPull;
    private PullToRefreshListView lvPtr;
    private TextView tvScroll;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_to_refresh_listview);

        cbScrollbar = (CheckBox) findViewById(R.id.cbScrollbar);
        cbHeaderPull = (CheckBox) findViewById(R.id.cbHeaderPull);
        cbFooterPull = (CheckBox) findViewById(R.id.cbFooterPull);
        lvPtr = (PullToRefreshListView)findViewById(R.id.lvPtr);
        tvScroll = (TextView) findViewById(R.id.tvScroll);

        tvScroll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lvPtr.setSelectionFromTop(lvPtr.getCount() - 1, 0);
            }
        });

        cbScrollbar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                adapter.setCount(isChecked ? 30 : 10);
            }
        });
        cbHeaderPull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                lvPtr.setHeaderRefreshEnabled(isChecked);
            }
        });
        cbFooterPull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                lvPtr.setFooterRefreshEnabled(isChecked);
            }
        });

        lvPtr.setHeaderRefreshEnabled(false);
        lvPtr.setFooterRefreshEnabled(false);
        lvPtr.setHeader(new Header(this));
        TextView header = new TextView(this);
        header.setText("我是额外的header，出现在下拉header下面");
        header.setTextColor(Color.RED);
        header.setGravity(CENTER);
        lvPtr.addHeaderView(header);
        lvPtr.setFooter(new Footer(this));
        adapter = new MyAdapter();
        lvPtr.setAdapter(adapter);
        lvPtr.setOnHeaderRefreshListener(new OnHeaderRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lvPtr.stopHeaderRefresh();
                    }
                }, 1500);
            }
        });
        lvPtr.setOnFooterRefreshListener(new OnFooterRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lvPtr.stopFooterRefresh();
                    }
                }, 1500);
            }
        });
        lvPtr.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToastUtil.show(parent.getContext(), "onItemClick position = " + position);
            }
        });
    }

    private static class MyAdapter extends BaseAdapter{

        private int count = 10;

        public void setCount(int count){
            this.count = count;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return count;
        }

        @Override
        public Object getItem(int position) {
            return "[ITEM" + position + "]";
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Context context = parent.getContext();
            LinearLayout result;
            if (convertView == null){
                result = new LinearLayout(context);
                result.setOrientation(LinearLayout.HORIZONTAL);
                TextView tv = new TextView(context);
                tv.setTextSize(18);
                result.addView(tv);
                result.setTag(tv);

                TextView tv2 = new TextView(context);
                tv2.setText("哈哈哈");
                result.addView(tv2);
                tv2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ToastUtil.show(v.getContext(), "哈哈哈");
                    }
                });
            }else{
                result = (LinearLayout) convertView;
            }
            String content = (String)getItem(position);
            TextView tv = (TextView)result.getTag();
            tv.setText(content);
            return result;
        }
    }

    public class Header extends LinearLayout implements PullToRefreshWidget {

        private TextView tv;

        public Header(Context context) {
            super(context);
            init();
        }

        public Header(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public Header(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }

        private void init(){
            tv = new TextView(getContext());
            tv.setTextSize(14);
            tv.setTextColor(Color.GREEN);
            setState(PullToRefreshWidgetState.Empty);
            tv.setGravity(CENTER_HORIZONTAL|BOTTOM);
            tv.setBackgroundColor(Color.parseColor("#ffffff"));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-2, -2);
            lp.gravity = CENTER;
            addView(tv, lp);
            setBackgroundColor(Color.parseColor("#aaaaaa"));
        }

        public void setHeight(int px){
            if (px < 0){
                px = 0;
            }
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, px);
            tv.setLayoutParams(lp);
        }

        public void setState(PullToRefreshWidgetState state){
            switch (state){
                case Empty:{
                    tv.setText("");
                }
                break;
                case PullToRefresh:{
                    tv.setText("pull to refresh");
                }
                break;
                case ReleaseToRefresh:{
                    tv.setText("release to refresh");
                }
                break;
                case Refreshing:{
                    tv.setText("refreshing");
                }
                break;
                case DoNothing:{

                }
                break;
                case Finished: {
                    tv.setText("finished");
                }
                break;
            }
        }

        @Override
        public void show(boolean visibility) {

        }
    }

    public static class Footer extends LinearLayout implements PullToRefreshWidget {

        private TextView tv;

        public Footer(Context context) {
            super(context);
            init();
        }

        public Footer(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public Footer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }

        private void init(){
            tv = new TextView(getContext());
            tv.setTextSize(14);
            tv.setTextColor(Color.GREEN);
            setState(PullToRefreshWidgetState.Empty);
            tv.setGravity(CENTER_HORIZONTAL|TOP);
            tv.setBackgroundColor(Color.parseColor("#ffffff"));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2);
            lp.gravity = CENTER;
            addView(tv, lp);
            setBackgroundColor(Color.parseColor("#aaaaaa"));
        }

        public void setHeight(int px){
            if (px < 0){
                px = 0;
            }
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, px);
            tv.setLayoutParams(lp);
        }

        public void setState(PullToRefreshWidgetState state){
            switch (state){
                case Empty:{
                    tv.setText("");
                }
                break;
                case PullToRefresh:{
                    tv.setText("pull to refresh");
                }
                break;
                case ReleaseToRefresh:{
                    tv.setText("release to refresh");
                }
                break;
                case Refreshing:{
                    tv.setText("refreshing");
                }
                break;
                case DoNothing:{

                }
                break;
                case Finished: {
                    tv.setText("finished");
                }
                break;
            }
        }

        @Override
        public void show(boolean visibility) {

        }
    }
}
